/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.lang.*;
import TO.AuthorTO;
import TO.BookTO;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author admin-pc
 */
public class AuthorDAOImpl implements AuthorDAO {

    Connection con = null;
    PreparedStatement ptmt = null;
    ResultSet rs = null;

    private static Logger log = Logger.getLogger(AuthorDAOImpl.class.getName());

    private final String INSERT = "insert into author values(seq.nextval,?)";
    private final String DELETE = "delete from author where authorid=?";
    private final String UPDATE = "update author set name=? where authorid=?";
    private final String FIND_ALL = "SELECT * FROM author";
    private final String FIND_BY_PK = "select * from author where authorid=?";

    AuthorDAOImpl() {

    }

    private Connection getConnection() throws SQLException {
        Connection conn;
        conn = ConnectionFactory.getInstance().getConnection();
        return conn;
    }

    public void add(AuthorTO authorTo) {
        try {
            con = getConnection();
            ptmt = con.prepareStatement(INSERT);
            //ptmt.setInt(1,authorTo.getAuthorId());
            ptmt.setString(1, authorTo.getAuthorName());
            ptmt.executeUpdate();
        }
        catch (SQLException e) {
            log.log(Level.SEVERE, "Exception: ", e);
        }
        finally {
            if (ptmt != null) {
                try {
                    ptmt.close();
                }
                catch (SQLException e) {
                    log.log(Level.SEVERE, "Exception: ", e);
                }
            }
        }

    }

    public void delete(int authorId) {
        try {
            con = getConnection();
            ptmt = con.prepareStatement(DELETE);
            ptmt.setInt(1, authorId);
            ptmt.executeUpdate();
        }
        catch (SQLException e) {
            log.log(Level.SEVERE, "Exception: ", e);
        }
        finally {
            if (ptmt != null) {
                try {
                    ptmt.close();
                }
                catch (SQLException e) {
                    log.log(Level.SEVERE, "Exception: ", e);
                }
            }
        }

    }

    public void update(AuthorTO authorTo) {
        try {
            con = getConnection();
            ptmt = con.prepareStatement(UPDATE);
            ptmt.setString(1, authorTo.getAuthorName());
            ptmt.setInt(2, authorTo.getAuthorId());
            ptmt.executeUpdate();
        }
        catch (SQLException e) {
            log.log(Level.SEVERE, "Exception: ", e);
        }
        finally {
            if (ptmt != null) {
                try {
                    ptmt.close();
                }
                catch (SQLException e) {
                    log.log(Level.SEVERE, "Exception: ", e);
                }
            }
        }

    }

    public List findAll() {
        List authors = new ArrayList<AuthorTO>();
        AuthorTO authorTo = null;
        try {
            con = getConnection();
            ptmt = con.prepareStatement(FIND_ALL);
            rs = ptmt.executeQuery();
            while (rs.next()) {
                authorTo = new AuthorTO();
                authorTo.setAuthorId(rs.getInt(1));
                authorTo.setAuthorName(rs.getString(2));
                authors.add(authorTo);
            }

        }
        catch (SQLException e) {
            log.log(Level.SEVERE, "Exception: ", e);
        }
        finally {
            if (ptmt != null) {
                try {
                    ptmt.close();
                }
                catch (SQLException e) {
                    log.log(Level.SEVERE, "Exception: ", e);
                }
            }
        }
        return authors;
    }

    public AuthorTO findByPK(int authorId) {
        AuthorTO authorTo = null;
        try {
            con = getConnection();
            ptmt = con.prepareStatement(FIND_BY_PK);
            ptmt.setInt(1, authorId);
            rs = ptmt.executeQuery();
            if (rs.next()) {
                authorTo = new AuthorTO();
                //  authorTo.setAuthorId(rs.getInt(1));
                authorTo.setAuthorName(rs.getString(2));
            }
        }
        catch (SQLException e) {
            log.log(Level.SEVERE, "Exception: ", e);
        }
        finally {
            if (ptmt != null) {
                try {
                    ptmt.close();
                }
                catch (SQLException e) {
                    log.log(Level.SEVERE, "Exception: ", e);
                }
            }
        }
        return authorTo;
    }

}
